﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Atom.biz;
using Atom.NetFrame;
using Atom.Tool;
using Protocol.dto;
using Protocol;

namespace Atom.Logic.Account
{
    public class AccountHandle : AbsOnceHandler, HandleInterface
    {
        private IAccountBiz accountBiz = BizFactory.AccountBiz;

        public void ClientClose(UserToken ut, string error)
        {
            ExecutorPool.Instance.Executor(
            delegate
            {
                accountBiz.Close(ut);
            }
            );
        }

        public void ClientConnect(UserToken ut)
        {

        }

        public void MessageReceive(UserToken ut, SocketModel message)
        {
            switch (message.Command)
            {
                case AccountProtocol.LOGIN_REQ:
                    Login(ut, ReqLogin.Decode(message.Message));
                    break;
                case AccountProtocol.REG_REQ:
                    Register(ut,ReqRegister.Decode(message.Message));
                    break;
            }

            Console.WriteLine("登陆模块收到消息" + "线程ID" + Thread.CurrentThread.ManagedThreadId);
        }

        /// <summary>
        /// 账号登录
        /// </summary>
        public void Login(UserToken ut, ReqLogin value)
        {
            ExecutorPool.Instance.Executor(
                delegate
                {
                    int result = accountBiz.Login(ut, value.UserName, value.PassWord);
                    ResLogin login = new ResLogin();
                    login.Result = result;
                    SendMsg(ut, AccountProtocol.LOGIN_ERS, login.Encode());
                }
                );
        }
        public void Register(UserToken ut, ReqRegister value)
        {
            ExecutorPool.Instance.Executor(
                delegate
                {
                    int result = accountBiz.Create(ut, value.UserName, value.PassWord);
                    ResRegister register = new ResRegister();
                    register.Result = result;
                    SendMsg(ut, AccountProtocol.REG_ERS, register.Encode());
                }
                );
        }

        public override int GetArea()
        {
            return 0;
        }

        public override byte Gettype()
        {
            return GameProtocol.TYPE_ACCOUNT;
        }



    }
}
